package resilienceManager.qmul.gemom;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

/**
 * The class ViewCanvas visualizes the threads in the monitor demonstration.
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu, Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class ViewCanvas extends Canvas
{
	/**
	 * The threads to visualize.
	 */
	private starThread[] threads;

	/**
	 * The width of the canvas.
	 */
	private int width;

	/**
	 * The height of the canvas.
	 */
	private int height;

	/**
	 * The offscreen image used for double buffering.
	 */
	private Image image;

	/**
	 * The graphics context of the offscreen image.
	 */
	private Graphics graphics;

	/**
	 * The current time in seconds.
	 */
	private int time;


	private static int start_point=50;

	private static int end_point=650;

	/**
	 * Creates a ViewCanvas object.
	 * @param threads the threads to visualize
	 */
	public ViewCanvas( starThread[] threads )
	{
		this.threads = threads;
		width = 600;
		height = 50*threads.length + 40;
		setSize( width, height );
	}

	/**
	 * Updates the canvas.
	 * @param g the graphics context to use
	 */
	public void update( Graphics g )
	{
		// create image
		if ( image == null ) {
			image = createImage( width, height );
			graphics = image.getGraphics();
		}

		// clear image
		if ( time == 0 ) {
			graphics.setColor( Color.white );
			graphics.fillRect( 0, 0, width, height );
			graphics.setColor( Color.black );
			for ( int i = 0; i < threads.length; i++ )
				graphics.drawLine( start_point, 71 + 50*i, end_point, 71 + 50*i );

			//graphics.setColor(Color.RED);
			//graphics.drawString("0", start_point, 85+50*(threads.length-1));
		}

		// draw activity of threads
		boolean active=false;
		for ( int i = 0; i < threads.length; i++ ) {
			boolean running = false;
			Color color = Color.white;
			switch ( threads[i].getcurrentState() ) {
			case starThread.RUNNING:
				running = true; color = Color.green; active=true; break;
			case starThread.PAUSE:
				running = false; color = Color.green; active=true;break;
			case starThread.SYNCHRONIZATION:
				running = true; color = Color.red; active=true;break;
			case starThread.STOP:
				running = false; color = Color.red; active=false;break;
			}
			graphics.setColor( color );


			int length=(int)threads[i].getCurrentValue();

			int leaf_num=threads[i].getLeaf(); 

			graphics.drawLine( start_point + time, 70 + 50*i,
					start_point + time, 70 + 50*i - (running?length:0) );

		}

		// draw image
		g.drawImage( image, 0, 0, this );
		if(active)
		{
			time = (time + 1)%600;
		}

		int syn=time%50;
		if(syn==0)
		{
			for ( int i = 0; i < threads.length; i++ ) {
				boolean running = true;
				Color color = Color.RED;

				if((threads[i].getcurrentState()!=starThread.PAUSE)
						||(threads[i].getcurrentState()!=starThread.STOP))
				{
					threads[i].synThread(threads);
					graphics.setColor( color );


					int length=(int)threads[i].getCurrentValue();

					int leaf_num=threads[i].getLeaf(); 

					graphics.drawLine( start_point + time, 70 + 50*i,
							start_point + time, 70 + 50*i - (running?length:0) );
				}
			}

			time = (time + 1)%600;
		}

	}

	/**
	 * Paints the canvas.
	 * @param g the graphics context to use
	 */
	public void paint( Graphics g )
	{
		if ( image != null )
			g.drawImage( image, 0, 0, this );
	}
}

